/*
Copyright (c) 2013 Mihail Volkov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef EVENTSENGINE_H_INCLUDED
#define EVENTSENGINE_H_INCLUDED

//
// A helper engine that allows to post and retrieve events
// in way applicable to update/poll model
//

#include <list>
#include <cassert>
#include <cstdlib>

class EventBase
{
	template <class EventClass>
	friend class Event;
	friend class EventsTracker;

protected:
	virtual EventBase **getCurrentUpdateSlot () = 0;
	virtual EventBase **getNextUpdateSlot () = 0;

public:
	virtual ~EventBase () {}
};

/// @brief An events tracker. A singleton. Create one thing per app.
class EventsTracker
{
	template <class EventClass>
	friend class Event;

	static EventsTracker *spInstance;
	typedef std::list<EventBase *> EventsList;
	EventsList
		mCurrentEvents,mNextEvents;

	static void deleteEvents (EventsList &);

public:
	EventsTracker ();
	~EventsTracker ();

	/// @brief Perform next update cycle. All standing events of previous
	/// update cycle will be dropped, and all events posted during it will
	/// take their place.
	static void update ();

	/// @brief Post an event to next update cycle. It will be available via
	/// corresponding event class' poll method on next update cycle.
	/// Note that posting next event of the same class during the same
	/// update cycle will overwrite previously posted one.
	/// The pointer is assumed to point to new-allocated event object and
	/// the ownership of it is transferred from the caller.
	static void post (EventBase *);
};

/// @brief Template for defining events. An event type is identified
/// as a C++ class. Use: class YourEvent: public Event&lt;YourEvent&gt;
template <class EventClass>
class Event: public EventBase
{
	friend class EventsTracker;

	static EventBase **getCurrentUpdateSlotStatic ()
	{
		assert (EventsTracker::spInstance!=NULL);
		static std::list<EventBase *>::iterator it =
			EventsTracker::spInstance->mCurrentEvents.insert
			(EventsTracker::spInstance->mCurrentEvents.begin (),
			(EventBase *)NULL);
		return &(*it);
	}
	static EventBase **getNextUpdateSlotStatic ()
	{
		assert (EventsTracker::spInstance!=NULL);
		static std::list<EventBase *>::iterator it =
			EventsTracker::spInstance->mNextEvents.insert
			(EventsTracker::spInstance->mNextEvents.begin (),
			(EventBase *)NULL);
		return &(*it);
	}

	EventBase **getCurrentUpdateSlot ()
	{
		return getCurrentUpdateSlotStatic ();
	}
	EventBase **getNextUpdateSlot ()
	{
		return getNextUpdateSlotStatic ();
	}

public:
	/// @brief Get pointer to event of this class posted to current cycle.
	/// If none were posted before last update, NULL will be returned.
	/// The pointer will be invalidated after next update.
	static EventClass *poll ()
	{
		return (EventClass *)*getCurrentUpdateSlotStatic ();
	}
};


#endif // EVENTSENGINE_H_INCLUDED
